package cn.hzking.gjmall.activitys.wuliu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fingdo.statelayout.StateLayout;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.my.AddressListActivity;
import cn.hzking.gjmall.adpters.WuliuDetailAdapter;
import cn.hzking.gjmall.base.ColorStatuBarActivity;
import cn.hzking.gjmall.bean.WuliuDetailBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseEntity;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.utils.ListUtils;
import cn.hzking.gjmall.utils.TimeManagement;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class WuliuDetailActivity extends ColorStatuBarActivity {
    @Bind(R.id.rv) RecyclerView rv;
    @Bind(R.id.rl_back) RelativeLayout backRl;
    @Bind(R.id.sl) StateLayout sl;
    @Bind(R.id.tv_wuliuno) TextView wuliunoTv;
    @Bind(R.id.tv_company) TextView companyTv;
    @Bind(R.id.tv_state) TextView stateTv;
    private WuliuDetailAdapter adapter;
    private int orderId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_wuliu_detail);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        if(getIntent()!=null){
            orderId=getIntent().getIntExtra("orderId",-1);
        }
        init();
    }

    private void init() {
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        adapter=new WuliuDetailAdapter(R.layout.item_wuliudetail,null);
        rv.setNestedScrollingEnabled(false);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);
        initData();
    }

    private void initData() {
        RetrofitFactory.getInstant().API()
                .wuliuDetail(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<WuliuDetailBean>(this,true) {

                    @Override
                    protected void onSuccees(WuliuDetailBean wuliuDetailBean) throws Exception {
                        if(wuliuDetailBean.getData()!=null){
                            setData(wuliuDetailBean.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        if(isNetWorkError){
                            UIUtils.showToastShort(WuliuDetailActivity.this,getResources().getString(R.string.net_error));
                        }else {
                            UIUtils.showToastShort(WuliuDetailActivity.this,"服务器错误");
                        }
                    }

                    @Override
                    protected void onCodeError(WuliuDetailBean wuliuDetailBean) throws Exception {
                        super.onCodeError(wuliuDetailBean);
                        UIUtils.showToastShort(WuliuDetailActivity.this,wuliuDetailBean.getErrorMsg());
                    }
                });
    }

    private void setData(WuliuDetailBean.DataBean data) {
        List<WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean> list=new ArrayList<>();
        long cutime=System.currentTimeMillis()-data.getOrderCreateTime();
        if(cutime<=60*1000){//一分钟以内
            View view = View.inflate(WuliuDetailActivity.this,R.layout.layout_wuliu_empty,null);
            sl.showCustomView(view);
        }else if((System.currentTimeMillis()-data.getOrderCreateTime()<30*60*1000)){//1-30分钟
            list.clear();
            WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean contentBean=new WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean();
            contentBean.setContext("订单正在处理中");
            long dealtime=data.getOrderCreateTime()+60*1000;
            try {
                contentBean.setTime(TimeManagement.getExactTime(dealtime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            list.add(contentBean);
        }else {
            list.clear();
            if(!ListUtils.isEmpty(data.getLogisticsData())){
                WuliuDetailBean.DataBean.LogisticsDataBean logisticsDataBean=data.getLogisticsData().get(0);
                if(logisticsDataBean!=null){
                    if(!ListUtils.isEmpty(logisticsDataBean.getContent())){
                        list.addAll(data.getLogisticsData().get(0).getContent());
                    }
                    companyTv.setText("物流公司: "+logisticsDataBean.getResendCompany());
                    wuliunoTv.setText("运单编号: "+logisticsDataBean.getResendCode());
                }
            }

            WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean contentBean2=new WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean();//按时间倒序添加
            contentBean2.setContext("订单已推送至仓库");
            long sendtime=data.getOrderCreateTime()+30*60*1000;
            try {
                contentBean2.setTime(TimeManagement.getExactTime(sendtime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            list.add(contentBean2);

            WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean contentBean=new WuliuDetailBean.DataBean.LogisticsDataBean.ContentBean();
            contentBean.setContext("订单正在处理中");
            long dealtime=data.getOrderCreateTime()+60*1000;
            try {
                contentBean.setTime(TimeManagement.getExactTime(dealtime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            list.add(contentBean);
        }
        adapter.setNewData(list);
    }
}
